/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-01-08     zylx         first version
 */

#include <board.h>

#ifdef BSP_USING_LCD
#include <lcd_port.h>
#include <drv_spi.h>

//#define DRV_DEBUG
#define LOG_TAG             "drv.lcd"
#include <drv_log.h>

#define LCD_DEVICE(dev)     (struct drv_lcd_device*)(dev)

struct drv_lcd_device
{
    struct rt_device parent;

    struct rt_device_graphic_info lcd_info;

    struct rt_mutex lcd_lock;
  
    struct rt_spi_device * spi_device;
};

struct drv_lcd_device _lcd = {0};

static void rst_lcd(void)
{
    rt_pin_mode(LCD_RST_GPIO_NUM, PIN_MODE_OUTPUT);

    rt_pin_write(LCD_RST_GPIO_NUM, PIN_LOW);
    rt_thread_mdelay(1);
    rt_pin_write(LCD_RST_GPIO_NUM, PIN_HIGH);
    rt_thread_mdelay(120);
}
void lcd_write_data(uint8_t dat)
{
  rt_spi_send(_lcd.spi_device, &dat, 1);
}

void lcd_write_reg(uint8_t dat)
{
  rt_pin_write(LCD_DC_GPIO_NUM, PIN_LOW);
  rt_spi_send(_lcd.spi_device, &dat, 1);
  rt_pin_write(LCD_DC_GPIO_NUM, PIN_HIGH);
}

static rt_err_t drv_lcd_init(struct rt_device *device)
{
    struct drv_lcd_device *lcd = LCD_DEVICE(device);
    /* nothing, right now */
    lcd = lcd;
    return RT_EOK;
}

static rt_err_t drv_lcd_control(struct rt_device *device, int cmd, void *args)
{
    struct drv_lcd_device *lcd = LCD_DEVICE(device);
    struct rt_device_rect_info *rect_info = (struct rt_device_rect_info *)args;

    RT_ASSERT(rect_info != RT_NULL);

    switch (cmd)
    {
    case RTGRAPHIC_CTRL_RECT_UPDATE:
    {
        rt_mutex_take(&_lcd.lcd_lock, RT_WAITING_FOREVER);
#ifdef LCD_VERT_SCREEN
        /* write the color frame buffer to lcd*/
        lcd_write_reg(0x2a);//列地址设置
        lcd_write_data((uint8_t)((rect_info->x + 52)>>8));
        lcd_write_data((uint8_t)(rect_info->x + 52));
        lcd_write_data((uint8_t)((rect_info->x + rect_info->width - 1 + 52)>>8));
        lcd_write_data((uint8_t)(rect_info->x + rect_info->width - 1 + 52));
        lcd_write_reg(0x2b);//行地址设置
        lcd_write_data((uint8_t)(rect_info->y + 40)>>8);
        lcd_write_data((uint8_t)(rect_info->y + 40));
        lcd_write_data((uint8_t)((rect_info->y + rect_info->height - 1 + 40)>>8));
#else
        /* write the color frame buffer to lcd*/
        lcd_write_reg(0x2a);//列地址设置
        lcd_write_data((uint8_t)((rect_info->x + 40)>>8));
        lcd_write_data((uint8_t)(rect_info->x + 40));
        lcd_write_data((uint8_t)((rect_info->x + rect_info->width - 1 + 40)>>8));
        lcd_write_data((uint8_t)(rect_info->x + rect_info->width - 1 + 40));
        lcd_write_reg(0x2b);//行地址设置
        lcd_write_data((uint8_t)(rect_info->y + 53)>>8);
        lcd_write_data((uint8_t)(rect_info->y + 53));
        lcd_write_data((uint8_t)((rect_info->y + rect_info->height - 1 + 53)>>8));
        lcd_write_data((uint8_t)(rect_info->y + rect_info->height - 1 + 53));
#endif
        lcd_write_reg(0x2c);//储存器写
        rt_spi_send(_lcd.spi_device, &_lcd.lcd_info.framebuffer[0], rect_info->width * rect_info->height * LCD_BITS_PER_PIXEL / 8);
        rt_mutex_release(&_lcd.lcd_lock);
    }
    break;

    case RTGRAPHIC_CTRL_GET_INFO:
    {
        struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;

        RT_ASSERT(info != RT_NULL);
        info->pixel_format  = lcd->lcd_info.pixel_format;
        info->bits_per_pixel= lcd->lcd_info.bits_per_pixel;
        info->width         = lcd->lcd_info.width;
        info->height        = lcd->lcd_info.height;
        info->framebuffer   = lcd->lcd_info.framebuffer;
    }
    break;
    }

    return RT_EOK;
}

#if defined(LCD_BACKLIGHT_USING_PWM)
void turn_on_lcd_backlight(void)
{
    struct rt_device_pwm *pwm_dev;

    /* turn on the LCD backlight */
    pwm_dev = (struct rt_device_pwm *)rt_device_find(LCD_PWM_DEV_NAME);
    /* pwm frequency:100K = 10000ns */
    rt_pwm_set(pwm_dev, LCD_PWM_DEV_CHANNEL, 10000, 10000);
    rt_pwm_enable(pwm_dev, LCD_PWM_DEV_CHANNEL);
}
#elif defined(LCD_BACKLIGHT_USING_GPIO)
void turn_on_lcd_backlight(void)
{
    rt_pin_mode(LCD_BL_GPIO_NUM, PIN_MODE_OUTPUT);

    rt_pin_write(LCD_BL_GPIO_NUM, PIN_HIGH);
}
#else
void turn_on_lcd_backlight(void)
{

}
#endif

rt_err_t stm32_lcd_init(struct drv_lcd_device *lcd)
{
    lcd_write_reg(0x11); 
    rt_thread_mdelay(120); 
    lcd_write_reg(0x36); 
#ifdef LCD_VERT_SCREEN
    lcd_write_data(0x40);
#else
    lcd_write_data(0x70);
#endif

    lcd_write_reg(0x3A);
    lcd_write_data(0x05);

    lcd_write_reg(0xB2);
    lcd_write_data(0x0C);
    lcd_write_data(0x0C);
    lcd_write_data(0x00);
    lcd_write_data(0x33);
    lcd_write_data(0x33);

    lcd_write_reg(0xB7); 
    lcd_write_data(0x35);

    lcd_write_reg(0xBB);
    lcd_write_data(0x19);

    lcd_write_reg(0xC0);
    lcd_write_data(0x2C);

    lcd_write_reg(0xC2);
    lcd_write_data(0x01);

    lcd_write_reg(0xC3);
    lcd_write_data(0x12);

    lcd_write_reg(0xC4);
    lcd_write_data(0x20);

    lcd_write_reg(0xC6); 
    lcd_write_data(0x0F);

    lcd_write_reg(0xD0); 
    lcd_write_data(0xA4);
    lcd_write_data(0xA1);

    lcd_write_reg(0xE0);
    lcd_write_data(0xD0);
    lcd_write_data(0x04);
    lcd_write_data(0x0D);
    lcd_write_data(0x11);
    lcd_write_data(0x13);
    lcd_write_data(0x2B);
    lcd_write_data(0x3F);
    lcd_write_data(0x54);
    lcd_write_data(0x4C);
    lcd_write_data(0x18);
    lcd_write_data(0x0D);
    lcd_write_data(0x0B);
    lcd_write_data(0x1F);
    lcd_write_data(0x23);

    lcd_write_reg(0xE1);
    lcd_write_data(0xD0);
    lcd_write_data(0x04);
    lcd_write_data(0x0C);
    lcd_write_data(0x11);
    lcd_write_data(0x13);
    lcd_write_data(0x2C);
    lcd_write_data(0x3F);
    lcd_write_data(0x44);
    lcd_write_data(0x51);
    lcd_write_data(0x2F);
    lcd_write_data(0x1F);
    lcd_write_data(0x1F);
    lcd_write_data(0x20);
    lcd_write_data(0x23);

    //小端数据 为了适应littlevgl
    lcd_write_reg(0xB0);
    lcd_write_data(0x00);
    lcd_write_data(0xf8);

    lcd_write_reg(0x21); 

    lcd_write_reg(0x29); 
    LOG_D("lcd init success");
    return RT_EOK;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops lcd_ops =
{
    drv_lcd_init,
    RT_NULL,
    RT_NULL,
    RT_NULL,
    RT_NULL,
    drv_lcd_control
};
#endif
#define RT_ST7789VW_DEFAULT_SPI_CFG                                         \
{                                                                           \
    .mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB,                     \
    .data_width = 8,                                                        \
    .max_hz = 80000000,                                                     \
}
int drv_lcd_hw_init(void)
{
    rt_err_t result = RT_EOK;
    struct rt_device *device = &_lcd.parent;
    struct rt_spi_configuration cfg = RT_ST7789VW_DEFAULT_SPI_CFG;

    /* attach lcd to spi bus */
    rt_hw_spi_device_attach(LCD_SPI_BUS_NAME, "ST7789VW", GPIOE, GPIO_PIN_11);
  
    _lcd.spi_device = (struct rt_spi_device *)rt_device_find("ST7789VW");
    if (_lcd.spi_device == RT_NULL)
    {
        LOG_E("init spi devicee failed!\n");
        result = -RT_ERROR;
        goto __exit;
    }
    rt_spi_configure(_lcd.spi_device, &cfg);
    
    /* init lcd_lock mutex */
    result = rt_mutex_init(&_lcd.lcd_lock, "lcd_lock", RT_IPC_FLAG_FIFO);
    if (result != RT_EOK)
    {
        LOG_E("init mutex failed!\n");
        result = -RT_ENOMEM;
        goto __exit;
    }

    /* config LCD dev info */
    _lcd.lcd_info.height = LCD_HEIGHT;
    _lcd.lcd_info.width = LCD_WIDTH;
    _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
    _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;

    /* malloc memory for Triple Buffering */
    _lcd.lcd_info.framebuffer = rt_malloc(LCD_BUF_SIZE);
    if (_lcd.lcd_info.framebuffer == RT_NULL)
    {
        LOG_E("init frame buffer failed!\n");
        result = -RT_ENOMEM;
        goto __exit;
    }

    /* memset buff to 0xFF */
    rt_memset(_lcd.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE);

    device->type    = RT_Device_Class_Graphic;
#ifdef RT_USING_DEVICE_OPS
    device->ops     = &lcd_ops;
#else
    device->init    = drv_lcd_init;
    device->control = drv_lcd_control;
#endif

    /* register lcd device */
    rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);

    rst_lcd();
    rt_pin_mode(LCD_DC_GPIO_NUM, PIN_MODE_OUTPUT);
    rt_pin_write(LCD_DC_GPIO_NUM, PIN_HIGH);
    /* init stm32 LTDC */
    if (stm32_lcd_init(&_lcd) != RT_EOK)
    {
        result = -RT_ERROR;
        goto __exit;
    }
    else
    {
        struct rt_device_rect_info rect_info = {
                .x = 0,
                .y = 0,
                .width = LCD_WIDTH,
                .height = LCD_BUF_SIZE / LCD_WIDTH * 8 / LCD_BITS_PER_PIXEL,
        };
        while(1)
        {
            if(rect_info.y + rect_info.height >= LCD_HEIGHT)
            {
                rect_info.height = LCD_HEIGHT - rect_info.y;
                _lcd.parent.control(&_lcd.parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
                break;
            }
            else
            {
                _lcd.parent.control(&_lcd.parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
                rect_info.y += rect_info.height;
            }
        }
        turn_on_lcd_backlight();
    }

__exit:
    if (result != RT_EOK)
    {
        rt_mutex_delete(&_lcd.lcd_lock);

        if (_lcd.lcd_info.framebuffer)
        {
            rt_free(_lcd.lcd_info.framebuffer);
        }
    }
    return result;
}
INIT_DEVICE_EXPORT(drv_lcd_hw_init);

#ifdef DRV_DEBUG
#ifdef FINSH_USING_MSH
int lcd_test()
{
    struct drv_lcd_device *lcd;
    lcd = (struct drv_lcd_device *)rt_device_find("lcd");
    struct rt_device_rect_info rect_info = {
                    .x = 0,
                    .y = 0,
                    .width = LCD_WIDTH,
                    .height = LCD_HEIGHT
            };

    while (1)
    {
        /* red */
        for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
        {
            lcd->lcd_info.framebuffer[2 * i] = 0x00;
            lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
        }
        lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
        rt_thread_mdelay(500);
        /* green */
        for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
        {
            lcd->lcd_info.framebuffer[2 * i] = 0xE0;
            lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
        }
        lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
        rt_thread_mdelay(500);
        /* blue */
        for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
        {
            lcd->lcd_info.framebuffer[2 * i] = 0x1F;
            lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
        }
        lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
        rt_thread_mdelay(500);
    }
}
MSH_CMD_EXPORT(lcd_test, lcd_test);
#endif /* FINSH_USING_MSH */
#endif /* DRV_DEBUG */
#endif /* BSP_USING_LCD */
